/**
 * FileName: AuthorityActivity
 * Author: Administrator
 * Date: 2020/12/17 0017 16:08
 * Description:
 */
package com.linkage.linkedlife.ui.activity

import android.os.Bundle
import android.util.Log
import android.view.View
import androidx.databinding.DataBindingUtil
import cn.bmob.v3.BmobQuery
import cn.bmob.v3.exception.BmobException
import cn.bmob.v3.listener.FindListener
import cn.bmob.v3.listener.SaveListener
import cn.bmob.v3.listener.UpdateListener
import com.alibaba.android.arouter.launcher.ARouter
import com.bumptech.glide.Glide
import com.bumptech.glide.load.engine.DiskCacheStrategy
import com.clark.dingding.constants.ARouterPath
import com.linkage.linkedlife.base.BaseActivity
import com.google.android.material.snackbar.Snackbar
import com.linkage.linkedlife.R
import com.linkage.linkedlife.databinding.ActivityAuthorBinding
import com.linkage.linkedlife.model.AuthorCode
import com.linkage.linkedlife.utils.AndroidUtils

/**
 * @ClassName: AuthorityActivity
 * @Description: java类作用描述
 * @Author: Clark
 * @Date: 2020/12/17 0017 16:08
 */
class AuthorityActivity : BaseActivity() {
    lateinit var activityBinding: ActivityAuthorBinding
    public override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        activityBinding = DataBindingUtil.setContentView(this, R.layout.activity_author)
        //加载git动画
        Glide.with(this).load(R.drawable.top).diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(activityBinding.authorImg)

        val authorCode = AuthorCode()
        authorCode.phone = AndroidUtils.getAndroidId(this) //设备的id
        ARouter.getInstance().inject(this)
        activityBinding.checkCode.setOnClickListener { v: View? ->
            authorCode.checkCode = activityBinding.authorCode.text.toString()
            val bmobQuery = BmobQuery<AuthorCode>()
            bmobQuery.addWhereEqualTo("CheckCode", authorCode.checkCode)
            bmobQuery.findObjects(object : FindListener<AuthorCode>() {
                override fun done(list: List<AuthorCode>?, e: BmobException?) {
                    if (e == null && list?.size!! > 0 && list[0].isCodeValid) {
                        Snackbar.make(activityBinding.checkCode, "授权码有效", Snackbar.LENGTH_SHORT)
                            .show()
                        validCode(list[0], authorCode.phone)
                    } else {
                        Log.d("clark", e.toString())
                        Snackbar.make(activityBinding.checkCode, "授权码无效", Snackbar.LENGTH_SHORT)
                            .show()
                    }
                }
            })
        }

        activityBinding.skipCode.setOnClickListener { v: View ->
            goMainActivity()
        }


    }

    private fun validCode(authorCode: AuthorCode, phone: String) {
        authorCode.isCodeValid = false
        authorCode.phone = phone
        authorCode.update(authorCode.objectId, object : UpdateListener() {
            override fun done(e: BmobException) {
                if (e == null) {
                    Snackbar.make(activityBinding.checkCode, "更新成功", Snackbar.LENGTH_SHORT).show()
                    createNewCode()
                } else {
                    e.printStackTrace()
                    Snackbar.make(activityBinding.checkCode, "更新失败", Snackbar.LENGTH_SHORT).show()
                }
            }

        })
    }

    private fun createNewCode() {
        val authorCode = AuthorCode()
        authorCode.checkCode = AndroidUtils.getRandomString(6)
        authorCode.isCodeValid = true
        authorCode.save(object : SaveListener<String>() {
            override fun done(s: String, e: BmobException) {
                if (e == null) {
                    Snackbar.make(activityBinding.checkCode, "创建成功", Snackbar.LENGTH_SHORT).show()
                    goMainActivity()
                } else {
                    Snackbar.make(activityBinding.checkCode, "创建失败", Snackbar.LENGTH_SHORT).show()
                }
            }
        })
    }

    private fun goMainActivity() {
        ARouter.getInstance().build(ARouterPath.newsActivity).navigation()
    }
}